package jku.semtech.common;

import java.util.Calendar;

public class Player {
	private String dbpediaID;
	private String playerName;
	private FieldPosition position;
	private String description;
	private String birthDate;
	private String birthPlaceName;
	private String clubName;
	private String clubManagerName;
	private String realFieldPosition;
	private String picURL;
	private String clubEntryYear;
	private String goals;

	public Player(String name, FieldPosition position) {
		this.setDbpediaID(name.replace(' ', '_'));
		this.setName(name);
		this.setFieldposition(position);
	}

	// properties
	public String getDbpediaID() {
		return dbpediaID;
	}

	public void setDbpediaID(String dbpediaID) {
		this.dbpediaID = dbpediaID;
	}

	public String getName() {
		return playerName;
	}

	public void setName(String name) {
		this.playerName = name;
	}

	public String getFieldposition() {
		return position.toString();
	}

	public void setFieldposition(FieldPosition position) {
		this.position = position;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getBirthDate() {
		return birthDate;
	}

	public void setBirthDate(String birthDate) {
		this.birthDate = birthDate;
	}

	public String getBirthPlaceName() {
		return birthPlaceName;
	}

	public void setBirthPlaceName(String birthPlaceName) {
		this.birthPlaceName = organizeEntry(birthPlaceName);
	}

	public String getClubName() {
		return clubName;
	}

	public void setClubName(String clubName) {
		this.clubName = clubName;
	}

	public String getClubManagerName() {
		return clubManagerName;
	}

	public void setClubManagerName(String clubManagerName) {
		this.clubManagerName = clubManagerName;
	}

	public String getRealFieldPosition() {
		return realFieldPosition;
	}

	public void setRealFieldPosition(String realFieldPosition) {
		this.realFieldPosition = restructurePosition(realFieldPosition);
	}

	public String getPicURL() {
		return picURL;
	}

	public void setPicURL(String picURL) {
		this.picURL = picURL;
	}

	public String getClubEntryYear() {
		return clubEntryYear;
	}

	public void setClubEntryYear(String clubEntryYear) {
		this.clubEntryYear = clubEntryYear;
	}

	public String getAverageGoals() {
		return goals;
	}

	public void setGoals(String goals) {
		Calendar c = Calendar.getInstance();
		
		//calculate how many years the player is in the actual club
		int years = c.get(Calendar.YEAR) - Integer.parseInt(clubEntryYear);
		
		//if he is more than 1 year in the club
		//the goals have to be split up for each year
		if (years > 0) {
			int gpy = Integer.parseInt(goals) / years;
			this.goals = String.valueOf(gpy);
		} else {
			this.goals = goals;
		}
	}

	//neede to map the dbpedia positions to our positions
	private String restructurePosition(String position) {
		position = position.toLowerCase();

		if (position.contains("back") || position.contains("defender")) {
			return FieldPosition.DEFENSE.toString();
		} else if (position.contains("midfielder")) {
			return FieldPosition.MIDFIELD.toString();
		} else if (position.contains("goalkeeper")) {
			return FieldPosition.GOALKEEPER.toString();
		} else if (position.contains("striker") || position.contains("forward")) {
			return FieldPosition.ATTACK.toString();
		} else {
			return "Couldn't find position!";
		}
	}
	
	//remove underscores and other symbols from the given string
	private String organizeEntry(String entry) {
		entry = entry.replace("_", " ");
		int pos = entry.indexOf("@");
		if (pos == -1) {
			return entry;
		} else {
			return entry.substring(0, pos);
		}
	}
}
